/*
 * Package il.ac.biu.cs.grossmm.test.SimpleProtocol
 * File HelloRequest.java
 * 
 * This is a part of presence service framework API. 
 * See javadoc for more information.
 *  
 * Copyright (C) 2006 Grossmann Mark
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package il.ac.biu.cs.grossmm.foo.protocol;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class HelloRequest extends Request {
	String user;
	String digest;
	
	static MessageDigest d;
	static Lock lock = new ReentrantLock();
	
	static {
		try {
			d = MessageDigest.getInstance("SHA-1");
		} catch (NoSuchAlgorithmException e) {
			System.err.println(e.getMessage());
			System.exit(-1);
		}
	}
	
	public HelloRequest(String user, String password) {
		super();
		this.user = user;
		this.digest = encode(password);
	}
	
	
	protected HelloRequest(String tid, String[] params)
	{
		super(tid);
		this.user = params[0];
		this.digest = params[1];
	}
	
	static byte toHex(int n)
	{
		if (n < 10)
			return (byte) (n + '0');

		return (byte) (n + 'A' - 10);
	}

	

	/**
	 * @param input
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	private static String encode(String text)
	{
		byte[] input = text.getBytes();
		byte[] output;

		d.reset();
		output = d.digest(input);
		
		byte[] hex = new byte[output.length*2]; // 64

		for (int j = 0; j < output.length; j++) {
			byte b = output[j];
			hex[j * 2] = toHex((b & 0xf0) >> 4);
			hex[j * 2 + 1] = toHex(b & 0x0f);
		}

		String encoded = null;
		try {
			encoded = new String(hex, "ascii");
		} catch (UnsupportedEncodingException e) {
			assert false;
		}
		
		return encoded;
	}
	
	



	@Override
	public String getCommand()
	{
		return "HELLO";
	}

	@Override
	public String[] getParameters()
	{
		return new String[]{user, digest};
	}

	public static void understand() {
		RequestFactory fac = new RequestFactory() {
		public Request getRequest(String tid, String[] params)
			{
				return new HelloRequest(tid, params);
			}
		};
		
		factories.put("HELLO", fac);
	}

	public String getDigest()
	{
		return digest;
	}

	public String getUser()
	{
		return user;
	}
}
